<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间" prop="orderCreateTime">
        <el-date-picker clearable
                        v-model="queryParams.orderCreateTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库状态" prop="storeStatus">
        <el-select v-model="queryParams.storeStatus" placeholder="请选择入库状态" clearable>
          <el-option
            v-for="dict in dict.type.store_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库时间" prop="storeTime">
        <el-date-picker clearable
                        v-model="queryParams.storeTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="打包状态" prop="packStatus">
        <el-select v-model="queryParams.packStatus" placeholder="请选择打包状态" clearable>
          <el-option
            v-for="dict in dict.type.pack_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="打包时间" prop="packTime">
        <el-date-picker clearable
                        v-model="queryParams.packTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择打包时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出库状态" prop="outboundStatus">
        <el-select v-model="queryParams.outboundStatus" placeholder="请选择出库状态" clearable>
          <el-option
            v-for="dict in dict.type.outbound_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库时间" prop="outboundTime">
        <el-date-picker clearable
                        v-model="queryParams.outboundTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="盘点状态" prop="stocktakingStatus">
        <el-select v-model="queryParams.stocktakingStatus" placeholder="请选择盘点状态" clearable>
          <el-option
            v-for="dict in dict.type.stocktaking_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点时间" prop="stocktakingTime">
        <el-date-picker clearable
                        v-model="queryParams.stocktakingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择盘点时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请寄件" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择申请寄件" clearable>
          <el-option
            v-for="dict in dict.type.send_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="寄件时间" prop="sendTime">
        <el-date-picker clearable
                        v-model="queryParams.sendTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择寄件时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 替换原有 el-table 部分 -->
    <div v-loading="loading" class="card-container">
      <!-- 多选操作栏（保留表格的多选功能） -->
      <div v-if="multiple" class="selection-bar">
        <el-checkbox v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        <span class="selected-count">已选择 {{ selectedIds.length }} 项</span>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >批量删除</el-button>
      </div>

      <!-- 卡片列表 -->
      <el-row :gutter="20">
        <el-col
          v-for="(item, index) in infoList"
          :key="item.id"
          :xs="24" :sm="12" :md="8" :lg="6"
        >
          <el-card class="order-card" shadow="hover">
            <!-- 选择框 -->
            <div class="card-checkbox" v-if="multiple">
              <el-checkbox
                v-model="selectedIds"
                :label="item.id"
              ></el-checkbox>
            </div>

            <!-- 卡片头部 -->
            <div class="card-header">
              <span class="order-id">#{{ item.id }}</span>
              <el-tag
                size="mini"
                :type="statusTagType(item.orderStatus)"
                effect="dark"
              >
                {{ dictLabel('order_status', item.orderStatus) }}
              </el-tag>
            </div>

            <!-- 基本信息 -->
            <div class="base-info">
              <div class="info-item">
                <label>订单编号:</label>
                <span>{{ item.orderNumber }}</span>
              </div>
              <div class="info-item">
                <label>用户名称:</label>
                <span>{{ item.userName }}</span>
              </div>
              <div class="info-item">
                <label>下单时间:</label>
                <span>{{ parseTime(item.orderCreateTime) }}</span>
              </div>
            </div>

            <!-- 状态时间轴 -->
            <el-timeline class="status-timeline">
              <el-timeline-item
                v-for="status in statusList"
                :key="status.prop"
                :color="getStatusColor(item[status.prop])"
                placement="top"
              >
                <div class="status-item">
                  <span class="status-label">{{ status.label }}：</span>
                  <dict-tag
                    :options="dict.type[status.dictType]"
                    :value="item[status.prop]"
                    size="mini"
                  />
                  <div v-if="item[status.timeProp]" class="status-time">
                    {{ parseTime(item[status.timeProp], '{y}-{m}-{d}') }}
                  </div>
                </div>
              </el-timeline-item>
            </el-timeline>

            <!-- 操作按钮 -->
            <div class="card-actions">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="handleUpdate(item)"
                v-hasPermi="['system:info:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(item)"
                v-hasPermi="['system:info:remove']"
              >删除</el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 分页（保留原有分页组件） -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单操作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <div class="dynamic-form">
        <!-- 表单切换内容 -->
        <transition name="el-fade-in" mode="out-in">
          <!-- 基本信息模块 -->
          <el-form
            v-if="activeTab === 'basic'"
            key="basic"
            :model="form"
            label-width="100px"
            class="form-section"
            :rules="rules"
            ref="basicForm"
          >
            <el-form-item label="订单编号" prop="orderNumber">
              <el-input v-model="form.orderNumber" placeholder="请输入订单编号" style="width: 217px"/>
            </el-form-item>
            <el-form-item label="订单类型" prop="orderType">
              <el-select v-model="form.orderType" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.order_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="用户名称" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名称" style="width: 217px"/>
            </el-form-item>
            <el-form-item label="下单时间" prop="orderCreateTime">
              <el-date-picker
                v-model="form.orderCreateTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择下单时间"
                style="width: 217px"
              />
            </el-form-item>
            <el-form-item label="订单状态">
              <el-radio-group v-model="form.orderStatus">
                <el-radio
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"

                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>

          <!-- 状态信息模块 -->
          <el-form
            v-else
            key="status"
            :model="form"
            label-width="100px"
            class="form-section"
            :rules="rules"
            ref="statusForm"
          >
            <el-form-item label="订单状态">
              <el-radio-group v-model="form.orderStatus">
                <el-radio
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"

                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="入库状态" style="margin-top: 8px">
              <el-radio-group v-model="form.storeStatus">
                <el-radio
                  v-for="dict in dict.type.store_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="入库时间">
              <el-date-picker
                v-model="form.storeTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择入库时间"
              />
            </el-form-item>
            <el-form-item label="打包状态">
              <el-radio-group v-model="form.packStatus">
                <el-radio
                  v-for="dict in dict.type.pack_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="打包时间">
              <el-date-picker
                v-model="form.packTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择打包时间"
              />
            </el-form-item>

            <el-form-item label="出库状态">
              <el-radio-group v-model="form.outboundStatus">
                <el-radio
                  v-for="dict in dict.type.outbound_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="出库时间">
              <el-date-picker
                v-model="form.outboundTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择出库时间"
              />
            </el-form-item>

            <el-form-item label="盘点状态">
              <el-radio-group v-model="form.stocktakingStatus">
                <el-radio
                  v-for="dict in dict.type.stocktaking_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="盘点时间">
              <el-date-picker
                v-model="form.stocktakingTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择盘点时间"
              />
            </el-form-item>

            <el-form-item label="申请寄件">
              <el-radio-group v-model="form.sendStatus">
                <el-radio
                  v-for="dict in dict.type.send_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="寄件时间">
              <el-date-picker
                v-model="form.sendTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择寄件时间"
              />
            </el-form-item>
          </el-form>
        </transition>

        <!-- 切换按钮组 -->
        <div class="button-group">
          <el-button
            :type="activeTab === 'basic' ? 'primary' : 'info'"
            @click="activeTab = 'basic'"
          >订单基本信息</el-button>
          <el-button
            :type="activeTab === 'status' ? 'primary' : 'info'"
            @click="activeTab = 'status'"
          >订单状态信息</el-button>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/info";

export default {
  name: "Info",
  dicts: ['order_status', 'pack_status', 'send_status', 'order_type', 'outbound_status', 'store_status', 'stocktaking_status'],
  data() {
    return {

      checkAll: false,
      selectedIds: [],
      statusList: [
        { label: '订单类型', prop: 'orderType', dictType: 'order_type' },
        { label: '入库状态', prop: 'storeStatus', dictType: 'store_status', timeProp: 'storeTime' },
        { label: '打包状态', prop: 'packStatus', dictType: 'pack_status', timeProp: 'packTime' },
        { label: '出库状态', prop: 'outboundStatus', dictType: 'outbound_status', timeProp: 'outboundTime' },
        { label: '盘点状态', prop: 'stocktakingStatus', dictType: 'stocktaking_status', timeProp: 'stocktakingTime' },
        { label: '寄件状态', prop: 'sendStatus', dictType: 'send_status', timeProp: 'sendTime' }
      ],


      activeTab: 'basic', // 当前激活的标签页
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单操作表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        orderType: null,
        orderStatus: null,
        orderCreateTime: null,
        storeStatus: null,
        storeTime: null,
        packStatus: null,
        packTime: null,
        outboundStatus: null,
        outboundTime: null,
        stocktakingStatus: null,
        stocktakingTime: null,
        sendStatus: null,
        sendTime: null,
        userName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
        orderCreateTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ],
        storeStatus: [
          { required: true, message: "入库状态不能为空", trigger: "blur" }
        ],
        packStatus: [
          { required: true, message: "打包状态不能为空", trigger: "blur" }
        ],
        outboundStatus: [
          { required: true, message: "出库状态不能为空", trigger: "blur" }
        ],
        stocktakingStatus: [
          { required: true, message: "盘点状态不能为空", trigger: "blur" }
        ],
        sendStatus: [
          { required: true, message: "申请寄件不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    // 全选处理
    handleCheckAllChange(val) {
      this.selectedIds = val ? this.infoList.map(item => item.id) : []
    },

    // 状态标签类型
    statusTagType(status) {
      const typeMap = {
        0: 'success',
        1: 'primary',
        2: 'warning',
        3: 'danger'
      }
      return typeMap[status] || 'info'
    },

    // 获取状态颜色
    getStatusColor(value) {
      const colorMap = {
        success: '#67C23A',
        warning: '#E6A23C',
        danger: '#F56C6C',
        info: '#909399'
      }
      return colorMap[value] || '#409EFF'
    },

    // 获取字典标签
    dictLabel(dictType, value) {
      const target = this.dict.type[dictType].find(item => item.value == value)
      return target ? target.label : ''
    },



    /** 查询订单操作列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        orderType: null,
        orderStatus: 1,
        orderCreateTime: null,
        storeStatus: 2,
        storeTime: null,
        packStatus: 1,
        packTime: null,
        outboundStatus: 1,
        outboundTime: null,
        stocktakingStatus: 1,
        stocktakingTime: null,
        sendStatus: 1,
        sendTime: null,
        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单操作";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单操作";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 根据当前标签获取对应的表单引用
      const formRef = this.activeTab === 'basic' ? 'basicForm' : 'statusForm';
      this.$refs[formRef].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单操作编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    toggleTab() {
      this.activeTab = this.activeTab === 'basic' ? 'status' : 'basic'
    }
  }
};
</script>

<style scoped>

.order-card {
  margin-bottom: 20px;
  position: relative;
  transition: all 0.3s;

&:hover {
   transform: translateY(-3px);
   box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
 }

.card-checkbox {
  position: absolute;
  right: 15px;
  top: 15px;
  z-index: 1;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;

.order-id {
  font-weight: bold;
  color: #303133;
}
}

.base-info {
  margin-bottom: 15px;

.info-item {
  display: flex;
  margin-bottom: 8px;
  font-size: 13px;

label {
  color: #909399;
  min-width: 60px;
  margin-right: 8px;
}

span {
  color: #606266;
}
}
}

.status-timeline {
  padding-left: 10px;

::v-deep .el-timeline-item__node {
  left: -8px;
}

.status-item {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

.status-label {
  min-width: 70px;
  color: #909399;
  font-size: 12px;
}

.status-time {
  width: 100%;
  font-size: 12px;
  color: #c0c4cc;
  margin-top: 2px;
}
}
}

.card-actions {
  margin-top: 15px;
  text-align: right;

.el-button {
  padding: 6px 8px;
}
}
}

/* 响应式调整 */
@media (max-width: 768px) {
  .order-card {
    margin-bottom: 15px;

  .card-actions {
    text-align: center;
  }
}
}


</style>
